cmake_minimum_required(VERSION 3.8)
project(motion_predictor)

# Project configuration macros
set(EPSILON_WORKSPACE_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
set(COMMON_PACKAGE_PATH ${EPSILON_WORKSPACE_ROOT}/common)
set(VEHICLE_MODEL_PACKAGE_PATH ${EPSILON_WORKSPACE_ROOT}/vehicle/vehicle_model)
set(FORWARD_SIMULATOR_PACKAGE_PATH ${EPSILON_WORKSPACE_ROOT}/planning/forward_simulator)

# Default to C++17
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 17)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# Find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_components REQUIRED)
find_package(visualization_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(std_msgs REQUIRED)
find_package(common QUIET)
find_package(vehicle_model QUIET)
find_package(forward_simulator QUIET)
# This is a header-only library, create an interface library
add_library(${PROJECT_NAME} INTERFACE)

# Set include directories for the interface library
target_include_directories(${PROJECT_NAME} INTERFACE
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>
)

# Add dependencies to the interface library
if(common_FOUND)
  target_link_libraries(${PROJECT_NAME} INTERFACE common::hkust_pl_common)
  message(STATUS "Linked with common package")
else()
  message(WARNING "common package not found, using fallback include paths")
  target_include_directories(${PROJECT_NAME} INTERFACE
    $<BUILD_INTERFACE:${COMMON_PACKAGE_PATH}/include>
  )
endif()

if(vehicle_model_FOUND)
  target_link_libraries(${PROJECT_NAME} INTERFACE vehicle_model::vehicle_model)
  message(STATUS "Linked with vehicle_model package")
else()
  message(WARNING "vehicle_model package not found, using fallback include paths")
  target_include_directories(${PROJECT_NAME} INTERFACE
    $<BUILD_INTERFACE:${VEHICLE_MODEL_PACKAGE_PATH}/include>
  )
endif()

if(forward_simulator_FOUND)
  target_link_libraries(${PROJECT_NAME} INTERFACE forward_simulator::forward_simulator)
  message(STATUS "Linked with forward_simulator package")
else()
  message(WARNING "forward_simulator package not found, using fallback include paths")
  target_include_directories(${PROJECT_NAME} INTERFACE
    $<BUILD_INTERFACE:${FORWARD_SIMULATOR_PACKAGE_PATH}/include>
  )
endif()

# Add ROS2 dependencies
ament_target_dependencies(${PROJECT_NAME} INTERFACE
  rclcpp
  rclcpp_components
  visualization_msgs
  sensor_msgs
  geometry_msgs
  std_msgs
)

# Install headers
install(DIRECTORY include/
  DESTINATION include
)

# Export the interface library
install(TARGETS ${PROJECT_NAME}
  EXPORT export_${PROJECT_NAME}
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib
  RUNTIME DESTINATION bin
)

# Export dependencies
ament_export_targets(export_${PROJECT_NAME} HAS_LIBRARY_TARGET)
set(EXPORT_DEPENDENCIES
  rclcpp
  rclcpp_components
  visualization_msgs
  sensor_msgs
  geometry_msgs
  std_msgs
)
if(common_FOUND)
  list(APPEND EXPORT_DEPENDENCIES common)
endif()
if(vehicle_model_FOUND)
  list(APPEND EXPORT_DEPENDENCIES vehicle_model)
endif()
if(forward_simulator_FOUND)
  list(APPEND EXPORT_DEPENDENCIES forward_simulator)
endif()
ament_export_dependencies(${EXPORT_DEPENDENCIES})

# Export include directories
ament_export_include_directories(include)

# Testing
if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()
